# -*- coding: utf-8 -*-
"""
师生关系管理视图 - 管理员端

包含管理员对师生关系的全局管理功能：
- 关系列表查看和筛选
- 关系详情和时间线查看
- 关系统计和概览
"""

from flask import Blueprint, render_template, request, jsonify, flash, redirect, url_for
from flask_login import login_required, current_user
from app.decorators import admin_required
from app.controllers.relation_controller import RelationController

# 创建关系管理蓝图
relations_bp = Blueprint('relations', __name__)

@relations_bp.route('/')
@login_required
@admin_required
def index():
    """
    师生关系列表页面

    支持按校区、状态、时间范围筛选
    支持分页和搜索
    """
    # 获取筛选参数
    campus_id = request.args.get('campus_id', type=int)
    status = request.args.get('status', '')
    start_date = request.args.get('start_date', '')
    end_date = request.args.get('end_date', '')
    keyword = request.args.get('keyword', '')
    page = request.args.get('page', 1, type=int)
    per_page = request.args.get('per_page', 20, type=int)

    # 获取关系列表
    pagination, total = RelationController.get_all_relations(
        campus_id=campus_id,
        status=status,
        start_date=start_date,
        end_date=end_date,
        search_keyword=keyword,
        page=page,
        per_page=per_page
    )

    if not pagination:
        flash('获取师生关系列表失败', 'error')
        return render_template('relations/index.html',
                             relations=[], pagination=None,
                             campuses=[], filter_params={})

    # 获取校区列表用于筛选
    from app.models.campus import Campus
    campuses = Campus.query.filter_by(status='active').all()

    # 保存筛选参数
    filter_params = {
        'campus_id': campus_id,
        'status': status,
        'start_date': start_date,
        'end_date': end_date,
        'keyword': keyword
    }

    return render_template('relations/index.html',
                         relations=pagination.items,
                         pagination=pagination,
                         campuses=campuses,
                         filter_params=filter_params)

@relations_bp.route('/detail/<int:relation_id>')
@login_required
@admin_required
def detail(relation_id):
    """
    师生关系详情页面

    显示关系的详细信息和完整时间线
    """
    result = RelationController.get_relation_detail(relation_id)

    if not result['success']:
        flash(result['message'], 'error')
        return redirect(url_for('relations.index'))

    return render_template('relations/detail.html',
                         relation_data=result['data'])

@relations_bp.route('/statistics')
@login_required
@admin_required
def statistics():
    """
    师生关系统计页面

    显示各种统计图表和数据分析
    """
    # 获取统计数据
    stats_result = RelationController.get_relations_statistics()
    overview_result = RelationController.get_relations_overview()

    if not stats_result['success'] or not overview_result['success']:
        flash('获取统计数据失败', 'error')
        return render_template('relations/statistics.html',
                             statistics={}, overview={})

    return render_template('relations/statistics.html',
                         statistics=stats_result['data'],
                         overview=overview_result['data'])

@relations_bp.route('/api/overview')
@login_required
@admin_required
def api_overview():
    """
    API：获取关系概览数据

    用于仪表板的AJAX请求
    """
    result = RelationController.get_relations_overview()
    return jsonify(result)

@relations_bp.route('/api/statistics')
@login_required
@admin_required
def api_statistics():
    """
    API：获取统计数据

    用于图表展示的AJAX请求
    """
    result = RelationController.get_relations_statistics()
    return jsonify(result)